package org.aka.test;

import android.app.Activity;
import android.content.Context;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.hardware.Sensor;
import android.os.Bundle;
import android.util.Log;

public class AkaActivity extends Activity implements SensorEventListener {
    /** Called when the activity is first created. */
	
	private SensorManager mSensorManager;
	private Sensor mSensor;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        if (mSensor == null) {
        	Log.i("AKA", "Sensor doesn't supported");
        } else {
        	Log.i("AKA", String.format("Name: %s, type: %d", mSensor.getName(), mSensor.getType()));
        }
                
        setContentView(R.layout.main);
    }

	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {
		Log.i("AKA", String.format("onAccuracyChanged: %d", accuracy));
	}

	@Override
	public void onSensorChanged(SensorEvent event) {
		Log.i("AKA", String.format("onSensorChanged: %d: v0: %f, v1: %f, v2: %f",
				event.timestamp, event.values[0], event.values[1], event.values[2]));
		double angles[] = Utils.acc2angles(event.values);
		Log.i("AKA", String.format("Angles: %f, %f, %f", angles[0], angles[1], angles[2]));
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		Log.i("AKA", "onPause");
		mSensorManager.unregisterListener(this);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		Log.i("AKA", "onResume");
		boolean ok = mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_NORMAL);
		Log.i("AKA", Boolean.toString(ok));
	}
}
